package com.zhu.service.edu.controller.admin;


import com.zhu.commons.utils.R;
import com.zhu.service.edu.pojo.vo.SubjectVo;
import com.zhu.service.edu.service.IEduSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/admin/edu/subject")
@Api(tags = "课程分类管理")
@Slf4j
public class EduSubjectController {
      @Autowired
      private IEduSubjectService subjectService;

      @ApiOperation(value = "excel批量导入课程分类")
      @PostMapping("/import")
      public R batchImports(@RequestParam(value = "file",required = true) MultipartFile file) {
          InputStream inputStream = null;
          try {
              inputStream = file.getInputStream();
          } catch (Exception e) {
              e.printStackTrace();
          }
          subjectService.batchImport(inputStream);
          return R.ok().message("批量导入成功");

      }

      @ApiOperation(value = "Excel课程列表")
      @PostMapping("nested-list")
      public R nestedList(){
        List<SubjectVo> list= subjectService.nestedList();
        if(list!=null){
            return R.ok().data("items",list);
        }
       return R.error().message("列表不存在");
      }
}
